﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsClassifierFreshnessMeasurement.QueryExtractor;
using NewsClassifierFreshnessMeasurement.Model;
using NewsClassifierFreshnessMeasurement.Shared;

namespace MyQueryExtractor
{

    class MyQueryExtractor : IQueryExtractor
    {
        protected int part;
        public MyQueryExtractor(int _part)
        {
            part = _part;
        }
        public MyQueryExtractor()
        {
        }
        char[] arraySpecCha = {'\"','~','`','!','@','#','$','%','^','&','*','(',')','-','_','+','=','[','{',']','}','\\','|'
                              ,';',':','\'',',','>','.','>','/','?'
                              ,'！','·','￥','…','（','）','—','、','·','；','：','‘','“','”','，','《','。','》','？',' '};
        string EraseSpecCha(string ss)
        {
            string s = ss;
            for (int i = 0; i < s.Count(); ++i)
            {
                foreach (char c in arraySpecCha)
                {
                    char tmp = s[i];
                    if (s[i] == c)
                    {
                        s = s.Remove(i, 1);
                        --i;
                        break;
                    }
                }
            }
            return s;
        }
        public List<NewsQuery> Extract(News newsItem)
        {
            List<NewsQuery> listNewsQuery = new List<NewsQuery>();
            //string[] arrayQuery = newsItem.Title.Split(new char[1] { ' ' });
            List<string> arrayQuery = new List<string>();
            string validTitle = EraseSpecCha(newsItem.Title);
            for (int i = 0; i < validTitle.Count(); ++i)
            {
                arrayQuery.Add(validTitle[i].ToString());
            }
            int onePartCount = arrayQuery.Count() / part;
            int rest = arrayQuery.Count() % part;
            List<string> tmpS = new List<string>();
            int index = 0;
            for (int i = 0; i < part; ++i)
            {
                int n = onePartCount;
                if (rest > 0)
                {
                    ++n;
                    --rest;
                }
                string s = "";
                for (int j = 0; j < n; ++j)
                {
                    s += arrayQuery[index];
                    ++index;
                }
                if(s != "")
                    tmpS.Add(s);
            }

            foreach (string s in tmpS)
            {
                NewsQuery newsQuery = new NewsQuery();
                newsQuery.Query = s;
                listNewsQuery.Add(newsQuery);
            }
            return listNewsQuery;
        }
    }


    [ID("450c8d96-d284-4609-bf70-0e0d6021a0c9")]
    [Description("Divide title into two parts")]
    class MyQueryExtractor2 : MyQueryExtractor
    {

        public MyQueryExtractor2()
        {
            part = 2;
        }
    }
    [ID("f0e97206-8c53-4042-b64b-0f061ec85321")]
    [Description("Divide title into three parts")]
    class MyQueryExtractor3 : MyQueryExtractor
    {
        public Guid ID { get; set; }
        public string Description { get; set; }

        public MyQueryExtractor3()
        {
            part = 3;
        }
    }
    [ID("39ead443-4c39-4d37-aa8d-4eb04c22d584")]
    [Description("Divide title into four parts")]
    class MyQueryExtractor4 : MyQueryExtractor
    {
        public Guid ID { get; set; }
        public string Description { get; set; }
        public MyQueryExtractor4()
        {
            part = 4;
        }
    }
}
